/*
 * Created by SharpDevelop.
 * User: Pierre-Alain DAVID
 * Date: 15/07/2006
 * Time: 18:49
 * 
 */

using System;
using System.Drawing;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace YaP.Forms
{
    /// <summary>
    /// Description of prvChn.
    /// </summary>
    public partial class PrivateForm
    {
        #region Constructor

        public PrivateForm(string _nom, int _num, ChannelStyle _monstyle, MainForm _monpapa)
        {
            //
            // The InitializeComponent() call is required for Windows Forms designer support.
            //
            InitializeComponent();
            nom = _nom;
            papa = _monpapa;
            monstyle = _monstyle;
            Text = nom;
            num = _num;
            MainThreadId = Thread.CurrentThread.ManagedThreadId;
            //
            // TODO: Add constructor code after the InitializeComponent() call.
            //
        }

        #endregion

        #region Private properties

        private string exRTF;
        private int LineCount;
        private int MainThreadId;
        private ChannelStyle monstyle;
        private MainForm papa;

        #endregion

        #region Public properties

        public string nom;
        public int num;

        #endregion

        #region Public Methods

        public void addmsg(string from, string _msg)
        {
            StringBuilder msg = new StringBuilder(_msg);
            while (msg.ToString().StartsWith(" "))
                msg.Remove(0, 1);

            while ((msg.ToString().EndsWith("\n")) || (msg.ToString().EndsWith(" ")) || (msg.ToString().EndsWith("\r")))
            {
                msg.Remove(msg.Length - 1, 1);
            }
            if (msg.ToString().StartsWith("ACTION"))
            {
                string _tmpmsg = msg.ToString().TrimStart("ACTION".ToCharArray());
                AddAction(from, msg.ToString());
            }
            else
            {
                AddMsg(from, msg.ToString());
            }
        }

        #endregion

        #region private Methods

        private string timestring()
        {
            return monstyle.timelimitbeg + DateTime.Now.Hour + monstyle.timesep + DateTime.Now.Minute +
                   monstyle.timelimitend;
        }

        private void AddAction(string _from, string _msg)
        {
            Thread Adder = new Thread(ircRichTextBox1.AddRTF);
            Adder.Start(timestring() + " " + _from + " " + _msg + "\n");
        }

        private void AddMsg(string _from, string _msg)
        {
            Thread Adder = new Thread(ircRichTextBox1.AddRTF);
            Adder.Start(timestring() + " " + monstyle.nicklimitbeg + _from + monstyle.nicklimitend + " " + _msg);
        }

        public void addnotice(string _from, string _msg)
        {
            while ((_msg.EndsWith("\n")) || (_msg.EndsWith(" ")) || (_msg.EndsWith("\r")))
            {
                _msg = _msg.TrimEnd("\n".ToCharArray());
                _msg = _msg.TrimEnd("\r".ToCharArray());
                _msg = _msg.TrimEnd(" ".ToCharArray());
            }
            if (Thread.CurrentThread.ManagedThreadId != MainThreadId)
            {
                SyncAddNotice delegme = addnotice_snc;
                BeginInvoke(delegme, new object[] {_from, _msg});
            }
            else
                addnotice_snc(_from, ref _msg);
        }

        private void addnotice_snc(string _from, ref string _msg)
        {
            _msg = _msg.TrimEnd("\n".ToCharArray());
            ircRichTextBox1.SelectionStart = ircRichTextBox1.Text.Length;
            ircRichTextBox1.SelectionProtected = false;
            ircRichTextBox1.SelectionColor = Color.Magenta;
            ircRichTextBox1.SelectedText = timestring() + " " + monstyle.nicklimitbeg + "NOTICE from " + _from +
                                           monstyle.nicklimitend + _msg + "\n";
            ircRichTextBox1.SelectionProtected = true;
            ircRichTextBox1.SelectionStart = ircRichTextBox1.Text.Length + 1;
            ircRichTextBox1.ScrollToCaret();
        }

        #endregion

        #region Generated Events

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char) Keys.Enter)
            {
                e.Handled = true;
                string tosend = textBox1.Text;
                ////history[indexhistory] = textBox1.Text;
                //if (indexhistory < 128)
                //    indexhistory += 1;
                //else
                //    indexhistory = 0;

                //tempindexhistory = indexhistory;
                papa.Treatline(tosend, nom);
                textBox1.Clear();
            }
        }


        private void prvChn_FormClosed(object sender, FormClosedEventArgs e)
        {
            papa.KillPrivate(this);
        }

        private void ircRichTextBox1_Click(object sender, EventArgs e)
        {
            textBox1.Focus();
        }

        private void ircRichTextBox1_DoubleClick(object sender, EventArgs e)
        {
            textBox1.Focus();
        }

        private void ircRichTextBox1_Enter(object sender, EventArgs e)
        {
            //textBox1.Focus();
        }

        #endregion

        #region Delegates

        public delegate void SyncAddNotice(string _from, ref string _msg);

        #endregion

        //delegates

        private void ircRichTextBox1_TextChanged(object sender, EventArgs e)
        {
            if (papa.ActiveMdiChild == this)
            {
                textBox1.Focus();
            }
        }

        private void ircRichTextBox1_SelectionChanged(object sender, EventArgs e)
        {
            //Clipboard.SetData(System.Windows.Forms.DataFormats.Text, ircRichTextBox1.SelectedText);
            //if (ircRichTextBox1.SelectedText != null)
            //  Clipboard.SetText(ircRichTextBox1.SelectedText);
        }

        private void ircRichTextBox1_Click_1(object sender, EventArgs e)
        {
            textBox1.Focus();
        }

        private void ircRichTextBox1_DoubleClick_1(object sender, EventArgs e)
        {
            textBox1.Focus();
        }

        #region Nested type: SyncAddRTf

        private delegate void SyncAddRTf(StringBuilder str);

        #endregion

        #region Rtf Const.

        private const string RTF_CRLF = "\\par\r\n";
        private const string RTF_EOD = "}\r\n";

        private const string RTF_HEADER = "{\\rtf1\\ansi\\deff0{\\fonttbl{\\f0\\fswiss\\fcharset0 Microsoft Arial;}}" +
                                          "{\\colortbl ;" +
                                          /* COLOR TABLE
             * */
                                          //Black
                                          "\\red0\\green0\\blue0;" +
                                          // Blue
                                          "\\red22\\green26\\blue1101;" +
                                          //Green
                                          "\\red50\\green115\\blue32;" +
                                          //Red
                                          "\\red230\\green0\\blue0;" +
                                          //Maroon
                                          "\\red74\\green39\\blue11;" +
                                          //Purple
                                          "\\red99\\green21\\blue121;" +
                                          //Orange
                                          "\\red241\\green114\\blue27;" +
                                          //Yellow
                                          "\\red255\\green255\\blue0;" +
                                          //Light green
                                          "\\red70\\green237\\blue11;" +
                                          //Teal
                                          "\\red8\\green108\\blue97;" +
                                          //tu
                                          "\\red91\\green247\\blue253;" +
                                          //Dark blue
                                          "\\red13\\green23\\blue120;" +
                                          //Fuscia
                                          "\\red255\\green0\\blue210;" +
                                          //Dark grey
                                          "\\red96\\green96\\blue96;" +
                                          //light grey
                                          "\\red200\\green200\\blue200;" +
                                          // White is 16
                                          "\\red255\\green255\\blue255;" +
                                          // burly wood is 17
                                          "\\red206\\green175\\blue101;" +
                                          "}" +
                                          "\\viewkind4\\uc1\\pard\\cf17\\lang1036\\f0\\fs18\\par";

        #endregion
    }
}